package com.accountant.travelaccountant;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseOperations1 {

	/* We have decided on the column names below */
	public static final String KEY_ROWID = "_id";
	public static final String KEY_NAME = "persons_name";
	public static final String KEY_HOTNESS = "persons_hotness";

	/* Creating the name for a database */
	private static final String DATABASE_NAME = "HotOrNotdb";
	private static final String DATABASE_TABLE = "peopleTable";
	private static final int DATABASE_VERSION = 1;

	private DBHelper ourHelper;
	private final Context ourContext;
	private SQLiteDatabase ourDatabase;

	private static class DBHelper extends SQLiteOpenHelper {

		public DBHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			String query="CREATE TABLE " + DATABASE_TABLE+" ("+
					KEY_ROWID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
					KEY_NAME+" TEXT NOT NULL, " +
					KEY_HOTNESS+" TEXT NOT NULL);";
			db.execSQL(query);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			String query="DROP TABLE IF EXISTS "+DATABASE_TABLE;
			db.execSQL(query);
			onCreate(db);			
		}		
	}
	
	public DatabaseOperations1 open(){
		ourHelper=new DBHelper(ourContext);
		ourDatabase=ourHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		ourHelper.close();
		
	}
	
	public long insertEntry(){
		ContentValues cv=new ContentValues();
		cv.put(KEY_NAME, "Dhiraj");
		cv.put(KEY_HOTNESS, "Yes");
		return ourDatabase.insert(DATABASE_TABLE, null, cv);
	}
	
	public long updateEntry(){
		ContentValues cv=new ContentValues();
		cv.put(KEY_NAME, "Dhiraj");
		cv.put(KEY_HOTNESS, "Yes");
		String whereClause=KEY_ROWID+"="+1;
		return ourDatabase.update(DATABASE_TABLE, cv, whereClause, null);
	}
	
	public String retrieve(){
		String[] columns=new String[]{KEY_ROWID,KEY_NAME,KEY_HOTNESS};
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID+"="+1, null, null, null, null);
		if(c!=null){
			c.moveToFirst();String name=c.getString(1);
			return  name;
		}
		return null;
	}
	

	public DatabaseOperations1(Context c) {
		ourContext = c;
	}
	
	public void delete(){
		ourDatabase.delete(DATABASE_TABLE, KEY_ROWID+"="+1, null);
	}
}

